//service统一出口
import Request from './request'
import { BASE_URL, TIMEOUT } from './request/config'

import localCache from '@/utils/cache'

const hyRequest = new Request({
  baseURL: BASE_URL,
  timeout: TIMEOUT,
  interceptors: {
    requestInterceptors: (config) => {
      //携带token的拦截
      const token = localCache.getCache('token')
      if (config && config.headers) {
        if (config && token) {
          config.headers.Authorization = `Bearer ${token}`
        }
      }
      //console.log('单个实例的请求成功的拦截')
      return config
    },
    requestInterceptorsCatch: (err) => {
      //console.log('单个实例的请求失败的拦截')
      return err
    },
    responseInterceptors: (res) => {
      //console.log('单个实例的响应成功的拦截')
      return res
    },
    responseInterceptorsCatch: (err) => {
      //console.log('单个实例的响应成功的拦截')
      return err
    }
  }
})

export default hyRequest
